package com.yunyao.common.web.util;

import com.yunyao.common.web.enums.ExceptionEnum;
import com.yunyao.common.web.enums.ResultMsgEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * @author xingjishuai
 */
@Data
public class YResult<T> implements Serializable {
    private static final long serialVersionUID = 2181179808748008906L;

    private int code;
    private String msg;
    private T data;


    public YResult() {
        this.code = ResultMsgEnum.SUCCESS.getCode();
        this.msg = ResultMsgEnum.SUCCESS.getMessage();
    }

    private YResult(T data) {
        this.code = ResultMsgEnum.SUCCESS.getCode();
        this.msg = ResultMsgEnum.SUCCESS.getMessage();
        this.data = data;
    }


    private YResult(ExceptionEnum exceptionEnum) {
        this.code = exceptionEnum.getCode();
        this.msg = exceptionEnum.getMessage();
    }

    private YResult(String msg) {
        this.code = ResultMsgEnum.ERROR.getCode();
        this.msg = msg;
    }

    private YResult(int code, String msg) {
        this.code =code;
        this.msg = msg;
    }

    private YResult(ExceptionEnum exceptionEnum, T data) {
        this.code = exceptionEnum.getCode();
        this.msg = exceptionEnum.getMessage();
        this.data = data;
    }

    public static <T> YResult<T> success() {
        return new YResult<>();
    }

    public static <T> YResult<T> success(T data) {
        return new YResult<>(data);
    }

    public static <T> YResult<T> error(ExceptionEnum exceptionEnum) {
        return new YResult<>(exceptionEnum);
    }

    /**
     * 状态码500，自定义错误信息msg
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> YResult<T> error(String msg) {
        return new YResult<>(msg);
    }


    public static <T> YResult<T> error(int code, String msg) {
        return new YResult<T>(code,msg);
    }
}
